package com.cgsd.heyu.widgets;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;

/**
 * @author xiaoxing
 * Description 说明
 * CreateData: 2020/11/27
 */
public class  CustomWebView extends WebView {

    public CustomWebView(@NonNull Context context) {
        super(context);
        init();
    }

    public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        //开启硬件加速
        setLayerType(View.LAYER_TYPE_HARDWARE,null);
        WebSettings webSettings = getSettings();
        //是否开启JS支持
        webSettings.setJavaScriptEnabled(true);
        //是否允许JS打开新窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //缩放至屏幕大小
        webSettings.setUseWideViewPort(true);
        //缩放至屏幕大小
        webSettings.setLoadWithOverviewMode(true);
        //是否支持缩放
        webSettings.setSupportZoom(true);
        //是否支持缩放变焦，前提是支持缩放
        webSettings.setBuiltInZoomControls(true);
        //是否隐藏缩放控件
        webSettings.setDisplayZoomControls(false);
        //是否允许访问文件
        webSettings.setAllowFileAccess(true);
        //是否节点缓存
        webSettings.setDomStorageEnabled(true);
        //是否数据缓存
        webSettings.setDatabaseEnabled(true);
        //是否要手势触发媒体
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        //是否自动加载图片
        webSettings.setLoadsImagesAutomatically(true);
        //设置编码格式
        webSettings.setDefaultTextEncodingName("UTF-8");
        //是否需要获取焦点
        webSettings.setNeedInitialFocus(true);
        //是否从网络获取资源
        webSettings.setBlockNetworkLoads(false);
        setBackgroundColor(0);
    }
}
